﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Reflection;

namespace TvViewer.settings
{
    public class ConfigEditor : System.Configuration.AppSettingsReader
    {
        public XmlDocument cfgDoc;
        public string docName = String.Empty;
        private XmlNode node = null;
        public System.Collections.Generic.Dictionary<string, string> AppSettings;

        public Dictionary<string, string> GetAppSettings()
        {
            AppSettings = new Dictionary<string, string>();

            if (cfgDoc == null)
            {
                cfgDoc = new XmlDocument();
                LoadConfigDoc(cfgDoc);
            }

            node = cfgDoc.SelectSingleNode("//userSettings");

            foreach (XmlNode elem in node.ChildNodes)
            {
                if (elem.NodeType != XmlNodeType.Comment)
                    AppSettings.Add((string)elem.Attributes["key"].Value, (string)elem.Attributes["value"].Value);
            }
            return AppSettings;
        }
        public bool SetValue(string key, string value)
        {
            if (cfgDoc == null)
            {
                cfgDoc = new XmlDocument();
                LoadConfigDoc(cfgDoc);
            }
            // retrieve the appSettings node 
            node = cfgDoc.SelectSingleNode("//userSettings");

            if (node == null)
            {
                throw new System.InvalidOperationException("appSettings section not found");
            }

            try
            {
                // XPath select setting "add" element that contains this key 	  
                XmlElement addElem = (XmlElement)node.SelectSingleNode("//add[@key='" + key + "']");
                if (addElem != null)
                {
                    addElem.SetAttribute("value", value);
                }
                // not found, so we need to add the element, key and value
                else
                {
                    XmlElement entry = cfgDoc.CreateElement("add");
                    entry.SetAttribute("key", key);
                    entry.SetAttribute("value", value);
                    node.AppendChild(entry);
                }
                //save it
                saveConfigDoc(cfgDoc, docName);
                return true;
            }
            catch
            {
                return false;
            }
        }

        private void saveConfigDoc(XmlDocument cfgDoc, string cfgDocPath)
        {
            try
            {
                XmlTextWriter writer = new XmlTextWriter(cfgDocPath, null);
                writer.Formatting = Formatting.Indented;
                cfgDoc.WriteTo(writer);
                writer.Flush();
                writer.Close();
                return;
            }
            catch
            {
                throw;
            }
        }

        public bool removeElement(string elementKey)
        {
            try
            {
                XmlDocument cfgDoc = new XmlDocument();
                LoadConfigDoc(cfgDoc);
                // retrieve the appSettings node 
                node = cfgDoc.SelectSingleNode("//appSettings");
                if (node == null)
                {
                    throw new System.InvalidOperationException("appSettings section not found");
                }
                // XPath select setting "add" element that contains this key to remove	  
                node.RemoveChild(node.SelectSingleNode("//add[@key='" + elementKey + "']"));

                saveConfigDoc(cfgDoc, docName);
                return true;
            }
            catch
            {
                return false;
            }
        }


        private XmlDocument LoadConfigDoc(XmlDocument cfgDoc)
        {


            //docName = ((Assembly.GetEntryAssembly()).GetName()).Name;
            docName = @"C:\Program Files (x86)\Team MediaPortal\MP-TvVierwer\TvViewer.exe";
            docName += ".config";

            cfgDoc.Load(docName);
            return cfgDoc;
        }


        public XmlDocument LoadConfigDoc(string exePath)
        {
            // load the config file 

            docName = exePath;
            docName += ".config";
            // comment out next line when ready to deploy
            //System.Windows.Forms.MessageBox.Show(docName);
            if (cfgDoc == null) cfgDoc = new XmlDocument();
            cfgDoc.Load(docName);
            return cfgDoc;
        }



    }

}
